package com.czl.elm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czl.elm.dao.SearchMapper;
import com.czl.elm.dao.ShopMapper;
import com.czl.elm.domain.*;
import com.czl.elm.service.IFoodService;
import com.czl.elm.service.IShopService;
import com.czl.elm.util.ActionResult;
import com.czl.elm.util.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuoleiChen
 * @since 2021-05-21
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMapper searchMapper;

    @Autowired
    private IFoodService foodService;



    /**
     * 搜索所有的商店
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public RespPageBean selectAllShop(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer size) {
        Page<Shop> page=new Page<>(currentPage,size);
        IPage<Shop> shop_res=shopMapper.selectAllshop(page);

        RespPageBean res=new RespPageBean(shop_res.getTotal(),shop_res.getRecords());
        return res;
    }


    /**
     * 搜索该商店的详细信息
     * @param shopid
     * @return
     */
    @Override
    public Shopdetails selectShopDetails(Integer shopid) {
        return shopMapper.selectShopDetails(shopid);
    }

    /**
     * 测试专用，添加商店的详细信息
     * @param shopdetails
     * @return
     */

    @Override
    public int addShopDetails(Shopdetails shopdetails) {
        return shopMapper.addShopDetails(shopdetails);
    }
    /**
     * 查询商店的菜单
     * @param shopid
     * @return
     */
    @Override
    public Map<String, Object> selectShopMenu(Integer shopid) {
        Map<String,Object> res=new HashMap<>();
        Map<String, List<String>> menu=searchMenu(shopid);
        List<Food> foodList=searchAllFoodByShop(shopid);
        for(String menuItem:menu.keySet()){
            List<Food> foodListItem=new ArrayList<>();
            for(String foodid:menu.get(menuItem)){
                for(Food food:foodList){
                    if(food.getFoodid().equals(foodid)){
                        foodListItem.add(food);
                        break;
                    }
                }
            }
            res.put(menuItem,foodListItem);
        }
        return res;
    }

    /**
     * 获取该餐厅的菜单list
     * @param shopid
     * @return
     */
    public List<Food> searchAllFoodByShop(Integer shopid){
        return searchMapper.searchAllFoodByShop(shopid);
    }

    /**
     * 查询餐厅所有菜单模块的所有菜单和菜编号
     * @param shopid
     * @return
     */

    public Map<String, List<String>> searchMenu(Integer shopid){
        return searchMapper.searchMenu(shopid);
    }

    /**
     * 查找该商店的基本信息
     * @param shopid
     * @return
     */
    @Override
    public Shop selectShopBasic(Integer shopid) {
        Shop res=shopMapper.selectShopBasicById(shopid);
        return res;
    }

    /**
     * 查找该商店的优惠信息
     * @param shopid
     * @return
     */
    @Override
    public ActionResult selectPreferential(Integer shopid) {
        ShopPreferential res=searchMapper.selectPreferentialByShopId(shopid);
        if(res==null)return ActionResult.success("获取成功");
        return ActionResult.success("获取成功",res.getPreferential());
    }

    /**
     * 测试专用，添加该商店的游戏
     * @param shopPreferential
     */
    @Override
    public void t_addShopPreferential(ShopPreferential shopPreferential) {
        searchMapper.t_addShopPreferential(shopPreferential);
    }

    @Override
    public ActionResult t_addRamdomFood(Integer shopId, Integer number) {

        for(int i=0;i<number;i++){
            Food food=new Food();
            food.setCostGroup(0);
            food.setEnable(1);
            food.setFoodDescription("测试");
            food.setFoodGood(100);
            food.setFoodDiscount(100);
            food.setShopid(shopId);
            food.setFoodInfo("测试");
            food.setFoodLabel("测试");
            food.setFoodLimit(-1);
            food.setFoodMaterial("测试");
            food.setFoodMessage("测试");
            food.setFoodName("测试");
            food.setFoodUrl("/food/chicken.jpg");
            food.setFoodPack(200);
            food.setFoodTop(1);
            food.setFoodRest(-1);
            food.setFoodMoney(1000);
            food.setFoodNumber(100);
            foodService.addFood(food);
        }
        return ActionResult.success("成功");
    }

    @Override
    public ActionResult t_addRamdomFoodMenu(Integer shopId) {
        FoodMenu foodMenu=new FoodMenu();
        Map<String,List<String>> map=new HashMap<>();

        List<Food> list=searchAllFoodByShop(shopId);
        List<String> res=new ArrayList<>();
        for(Food f:list){
            res.add(f.getFoodid());
        }
        map.put("热销",res);
        foodMenu.setShopid(shopId);
        foodMenu.setShopMap(map);

        foodService.addShopMenu(foodMenu);
        return ActionResult.success("final");
    }

    public List<Map<String, Object>> getShopByShopLabel(String shopLabel){
        return shopMapper.getShopByShopLabel(shopLabel);
    }
}
